<?php
/*
* 电子名片模板模块
*/
class Ecard_model extends CI_Model{
	public function __construct(){
		$this->load->database();
		
	}
	
	//模板信息
	public function ecardInfo($data){
		$this->db->select('id,title,info,master,thumb,status');
		if($query = $this->db->get_where('ecard',$data)){
			return $query->row_array();
		}
		else{
			return false;
		}
	
	}

	/**
	* 列出所有模板
	* $s:start $l: limit
	*/
	public function ecardList($s = 0, $l = 10){

		$this->db->limit($s,$l);
		$query = $this->db->get('ecard');
		return $query->result_array();
	}
	
	//添加模板
	public function addEcard($data){
		
		return $this->db->insert('ecard',$data);
	}
	
	//编辑模板
	public function editEcard($data,$id){
		$this->db->where('id',$id);
		return $this->db->update('ecard',$data);
	}
	
	//更新模板状态
	public function up_status($id,$status){
		$ids = explode(',',$id);
		$this->db->where_in('id',$ids);
		$this->db->update('ecard',$status);
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
	//删除模板
	public function deleteEcard($id){
		
		$ids = explode(',',$id);

		$this->db->where_in('id',$ids);
		$this->db->delete('ecard');
		$rows = $this->db->affected_rows();
		if($rows >= 1){
			//同时删除相关联的属性
			$this->db->where_in('eid',$ids);
			$this->db->delete('ecard_attr');
			return true;
		}else{
			return false;
		}

	}
	
	//模板的属性列表
	public function attrByEcard($ecardid){
		$this->db->where(array('eid'=>$ecardid));
		$this->db->order_by('order desc');
		$query = $this->db->get('ecard_attr');
		
		return $query->result_array();
	}
	
	//添加属性
	public function addAttr($data){
		if($this->db->insert('ecard_attr',$data)){
			return true;
		}
		return false;
	}
	
	
	//编辑属性
	public function editeAttr($data,$where){
		$this->db->where($where);
		return $this->db->update('ecard_attr',$data);
		
	}
	
	//删除属性
	public function deleteAttr($id){
		
		$ids = explode(',',$id);
		
		$this->db->where_in('id',$ids);
		$this->db->delete('ecard_attr');
		$rows = $this->db->affected_rows();
		if($rows >= 1){
			return true;
		}else{
			return false;
		}

	}
	
	
}